package jdbc;

import java.sql.*;

public class JDBCDemo2 {
    public static void main(String[] args) {
        // 使用反射机制去创建mysql驱动对象
        try{
            Class.forName("com.mysql.cj.jdbc.Driver");
            System.out.println("加载驱动成功");
            // 创建Connection对象去连接数据库
            String sqlUrl = "jdbc:mysql://localhost:3306/jdbc_study?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true";
            String user = "root";
            String password = "root";
            Connection conn = DriverManager.getConnection(sqlUrl, user, password);

            System.out.println("连接数据库成功");
            // 创建statement对象去操作数据库中的表数据
            Statement statement = conn.createStatement();

            // 要操作的sql语句
            String sql = "INSERT INTO userinfo(username,password,nickname,age) VALUES" +
                    "('张三','123456','法外狂徒',22)";

            int num = statement.executeUpdate(sql);
            if(num>0){
                System.out.println("插入成功");
            }

        }catch (Exception e){
            System.out.println("加载驱动失败"+e.getMessage());
        }

    }
}
